package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttributeCategory;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductAttributeCateDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeCategoryService;
import com.tulingxueyuan.mall.modules.pms.service.impl.PmsProductAttributeCategoryServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService attributeCategoryService;

    /*
     商品分类列表
     url:'/productAttribute/category/list',
    method:'get',
    params:params
    */
    @ApiOperation("商品分类列表查询")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getList(@RequestParam(value = "pageNum",defaultValue  ="1")  Integer pageNum,
                                                                         @RequestParam(value = "pageSize",defaultValue  ="5")  Integer pageSize){

        Page page= attributeCategoryService.list(pageNum,pageSize);

        return CommonResult.success(CommonPage.restPage(page));
    }
    /**
     *    添加商品类型
     *   url:'/productAttribute/category/create',
     *     method:'post',
     *     data:data
     */
    @ApiOperation("商品类型添加")
    @PostMapping (value = "/create")
    @ResponseBody
    public CommonResult create(PmsProductAttributeCategory pmsProductAttributeCategory) {
        boolean count = attributeCategoryService.add(pmsProductAttributeCategory);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *  商品类型修改
     *   return request({
     *     url:'/productAttribute/category/update/'+id,
     *     method:'post',
     *     data:data
     *   })
     */
    @ApiOperation("商品类型修改")
    @PostMapping (value = "/update/{id}")
    @ResponseBody
    public CommonResult update(PmsProductAttributeCategory pmsProductAttributeCategory) {
        boolean result = attributeCategoryService.updateById(pmsProductAttributeCategory);
        if (result) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *   删除商品类型
     *   return request({
     *     url:'/productAttribute/category/delete/'+id,
     *     method:'get'
     *   })
     */
    @ApiOperation("商品类型删除")
    @GetMapping (value = "/delete/{id}")
    @ResponseBody
    public CommonResult delete(@PathVariable("id") long id) {
        boolean count = attributeCategoryService.removeById(id);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *   筛选属性下拉的级联数据
     *   return request({
     *     url:'/productAttribute/category/list/withAttr',
     *     method:'get'
     *   })
     */
     @GetMapping(value = "/list/withAttr")
    public CommonResult getListWithAttr(){
        List<ProductAttributeCateDTO> list= attributeCategoryService.getListWithAttr();
        return CommonResult.success(list);
     }
}

